package ui.activity.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.ImageView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.mine.biz.ChangePhoneBiz;
import ui.activity.mine.component.DaggerChangePhoneComponent;
import ui.activity.mine.contract.ChangePhoneContract;
import ui.activity.mine.module.ChangePhoneModule;
import ui.activity.mine.presenter.ChangePhonePresenter;
import widget.ClearEditText;


/**
 * 更改手机号
 * Created by chenjian on 2017/8/4.
 */

public class ChangePhoneAct extends BaseAct implements ChangePhoneContract.View {


    @Inject
    ChangePhonePresenter presenter;
    @Inject
    ChangePhoneBiz biz;
    @BindView(R.id.change_phone_pwd)
    ClearEditText pwd;
    @BindView(R.id.change_phone_show)
    ImageView show;
    @BindView(R.id.change_phone_submit)
    Button submit;


    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_change_phone);
    }

    @Override
    protected void setup() {
        DaggerChangePhoneComponent.builder().changePhoneModule(new ChangePhoneModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_change_phone), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        Constants.activitys.clear();
        Constants.activitys.add(this);
        RxView.clicks(show)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.setPwd(pwd.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()));
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.submitPwd(pwd.getText().toString().trim()));
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDatePwd(boolean isShow) {
        if (isShow) {
            pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            show.setBackgroundResource(R.mipmap.pay_close);
        } else {
            pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            show.setBackgroundResource(R.mipmap.pay_open);
        }
    }
}
